import os

ModelFile = 'static_model.h'
OutFile = 'icd_data_model.h'

IncludeHead = """#ifndef ICD_DATA_MODEL_H_
#define ICD_DATA_MODEL_H_

#include <stdlib.h>
#include <static_model.h>
"""
IncludeTail = """
#endif /* ICD_DATA_MODEL_H_ */
"""

StructString = """
typedef struct {
    void *ptr;
    int addr;
    int bitMapFlag;
    int bitOffset;
    int bitNum;
}IcdDataModel;
"""

defineArrayStart = """
IcdDataModel icdDataArray[] = {
"""
defineArrayEnd = """};
"""

ptr = "NULL"
addr = "0"
bitMapFlag = "0"
bitOffset = "0"
bitNum = "0"
string = "%s,%s,%s,%s,%s\n" %("/*ICD_data_Point".ljust(64), "address", "Flag", "Offset", "bitNum*/")

fileList = []

ReadFile = open(ModelFile, 'r')
WriteFile = open(OutFile, 'w')

fileList.append(IncludeHead)
fileList.append(StructString)
fileList.append(defineArrayStart)
fileList.append(string)

lines = ReadFile.readlines();
for line in lines:
    #去除;字符
    line = line.replace(";",'')
    #提取关键字
    parts = line.split()
    num = len(parts);
    if (num != 3):
        continue
    if (parts[0] == "extern"):
        if (parts[1] == "DataAttribute"):
            ptr = "&" + parts[2]
            string = "{%s,%s,%s,%s,%s},\n" %(ptr.ljust(64), addr.ljust(7), bitMapFlag.ljust(4), bitOffset.ljust(6), bitNum.ljust(6))
            fileList.append(string)
#删除最后一行
fileList.pop()
string = "{%s,%s,%s,%s,%s}\n" %(ptr.ljust(64), addr.ljust(7), bitMapFlag.ljust(4), bitOffset.ljust(6), bitNum.ljust(6))
#重新写入文件
fileList.append(string)
# WriteFile.writelines(fileList)

fileList.append(defineArrayEnd)
fileList.append(IncludeTail)
WriteFile.writelines(fileList)
ReadFile.close()
WriteFile.close()